package com.tenton.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenton.MyException.DataNotExist;
import com.tenton.mapper.CommodityMapper;
import com.tenton.mapper.CommodityTypeMapper;
import com.tenton.pojo.Commodity;
import com.tenton.pojo.CommodityType;
import com.tenton.service.CommodityService;
import com.tenton.utils.Constants;
import com.tenton.utils.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    CommodityTypeMapper commodityTypeMapper;

    /**
     * 删除商品信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("商品Id为空");
        } else {
            return commodityMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * 新增商品信息
     *
     * @param commodity
     * @param session
     * @return
     */
    @Override
    public int insert(Commodity commodity, HttpSession session) throws DataNotExist {
        if (commodity == null) {
            throw new DataNotExist("商品信息为空");
        } else {
            String name = (String) session.getAttribute("name");
            commodity.setCreateName(name);
            Date date = new Date();
            commodity.setCreateTime(date);
            if (commodityTypeMapper.selectByName(commodity.getComType()) == null) {
                CommodityType commodityType = new CommodityType(commodity.getComType(),name,date);
                commodityTypeMapper.insert(commodityType);
            }
            return commodityMapper.insert(commodity);
        }
    }

    /**
     * 获取所有商品信息
     *
     * @param pn
     * @param model
     * @return
     */
    @Override
    public void getAll(Integer pn, ModelMap model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<Commodity> commodities = commodityMapper.selectAll();
        PageInfo<Commodity> pageInfo = new PageInfo<Commodity>(commodities);
        model.addAttribute("pageInfo", pageInfo);
    }

    /**
     * 查询商品信息
     *
     * @param id
     * @param model
     */
    @Override
    public void selectByPrimaryKey(Integer id, Model model) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("商品Id为空");
        } else {
            model.addAttribute("commodity", commodityMapper.selectByPrimaryKey(id));
        }
    }

    /**
     * 查询商品信息
     *
     * @param id
     */
    @Override
    public Commodity selectById(Integer id) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("商品Id为空");
        } else {
            return commodityMapper.selectByPrimaryKey(id);
        }
    }

    /**
     * 修改商品信息
     *
     * @param commodity
     * @param session
     * @return
     */
    @Override
    public boolean update(Commodity commodity, HttpSession session) throws DataNotExist {
        if (commodity == null) {
            throw new DataNotExist("商品信息为空");
        } else {
            String name = (String) session.getAttribute("name");
            commodity.setUpdateName(name);
            Date date = new Date();
            commodity.setUpdateTime(date);
            return commodityMapper.updateByPrimaryKey(commodity);
        }
    }

    /**
     * 多条件查询
     *
     * @param comName
     * @param pn
     * @param model
     */
    @Override
    public void getByParams(String comName, Integer pn, ModelMap model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<Commodity> commodities = commodityMapper.getByParams(comName);
        PageInfo<Commodity> pageInfo = new PageInfo<Commodity>(commodities);
        model.addAttribute("pageInfo", pageInfo);
    }

    /**
     * 获通过商品Id取商品信息
     *
     * @param comId
     * @param model
     */
    @Override
    public void getCommodityByComId(Integer comId, Model model) throws DataNotExist {
        if (IntegerUtils.isEmpty(comId)) {
            throw new NullPointerException("商品Id为空");
        } else {
            Commodity commodity = commodityMapper.selectByPrimaryKey(comId);
            if (commodity == null) {
                throw new DataNotExist("不存在此商品信息");
            } else {
                model.addAttribute("commodity", commodity);
            }
        }
    }
}
